/**
 * author: Maxim Dybarskiy
 * date:   May 17, 2011
 * time:   12:03:34 PM
 */
package com.hotel.web.controller;

import com.hotel.main.entity.Room;
import com.hotel.main.entity.enums.*;
import com.hotel.main.service.IAccommodationService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import javax.annotation.PostConstruct;
import javax.faces.model.SelectItem;

@Controller("accommodationControllerBean")
@Scope(value = "request")
public class AccommodationControllerBean {
    
    /* don`t like this hardcode way.
     * TODO: move into config file or something like that */
    private static final int FLOORS = 13;
    
    @Autowired
    private IAccommodationService accommodationService;
    private List<Room> rooms;
    private Integer selectedRoomId = -1;
    
    // enums properties
    private SelectItem[] floorValues = null;
    private SelectItem[] quantityValues = null;
    private SelectItem[] roomClassValues = null;
    
    // query params
    private RoomClass roomClass;
    private Byte floor = -1;
    private RoomQuantity quantity;
    
    @PostConstruct
    public void init() {
        floorValues = new SelectItem[FLOORS + 1];
        for(byte i = 0; i < FLOORS; i++) {
            floorValues[i] = new SelectItem((byte) i + 1, (i + 1) + "");
        }
        floorValues[FLOORS] = new SelectItem((byte) -1, "any");
        
        roomClassValues = new SelectItem[RoomClass.values().length];
        int i = 0;
        for(RoomClass rc: RoomClass.values()) {
            roomClassValues[i++] = new SelectItem(rc, rc.getLabel());
        }
        
        quantityValues = new SelectItem[RoomQuantity.values().length + 1];
        i = 0;
        for(RoomQuantity rq: RoomQuantity.values()) {
            quantityValues[i++] = new SelectItem(rq, rq.getLabel());
        }
        quantityValues[RoomQuantity.values().length] = new SelectItem(null, "any");
    }
    
    public void findRoom() {
        if (quantity != null && floor != -1) {
            rooms = accommodationService.findRoom(roomClass, floor, quantity);
        } else if (quantity == null) {
            rooms = accommodationService.findRoom(roomClass, floor);
        } else if (floor == -1) {
            rooms = accommodationService.findRoom(roomClass, quantity);
        } else {
            rooms = accommodationService.findRoom(roomClass);
        }
        
        /* just for test. TODO: delete it! */
        rooms = new ArrayList<Room>();
        Room r = new Room();
        r.setId(1l);
        r.setFloor((byte) 1);
        r.setRoomQuantity(RoomQuantity.DOUBLE.getQuantity());
        r.setRoomClass(RoomClass.STANDART);
        rooms.add(r);
        
        r = new Room();
        r.setId(2l);
        r.setFloor((byte) 2);
        r.setRoomQuantity(RoomQuantity.SINGLE.getQuantity());
        r.setRoomClass(RoomClass.SUITE);
        rooms.add(r);
        /* * */
    }
    
    /* query params` accessors */
    public Byte getFloor() {
        return floor;
    }
    
    public void setFloor(Byte floor) {
        this.floor = floor;
    }
    
    public RoomQuantity getQuantity() {
        return quantity;
    }
    
    public void setQuantity(RoomQuantity quantity) {
        this.quantity = quantity;
    }
    
    public RoomClass getRoomClass() {
        return roomClass;
    }
    
    public void setRoomClass(RoomClass roomClass) {
        this.roomClass = roomClass;
    }
    
    /* enums` values accessors */
    public SelectItem[] getFloorValues() {
        return floorValues;
    }
    
    public SelectItem[] getQuantityValues() {
        return quantityValues;
    }
    
    public SelectItem[] getRoomClassValues() {
        return roomClassValues;
    }
    
    /* result accessor */
    public List<Room> getRooms() {
        return rooms;
    }
    
    /* selected room accessors */
    public Integer getSelectedRoomId() {
        return selectedRoomId;
    }
    
    public void setSelectedRoomId(Integer selectedRoomId) {
        this.selectedRoomId = selectedRoomId;
        
        
    }
}
